<?php

namespace App\Jobs;

use App\Models\Wxuser;
use App\Models\WxuserStar;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SendTemplateMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $wxuser_start;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(WxuserStar $wxuserStar)
    {
        $this->wxuser_start = $wxuserStar;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $wxuser = Wxuser::find($this->wxuser_start->id_wxusers);
        //推送模板消息
        if(!empty($wxuser) && !empty($wxuser->openid)){
            $data =  [
                "touser"=>$wxuser->openid,
                "template_id"=>'xbc8yalt3MQW5lD3GeeAKAcjwhg6_5NiD1hNNUOiCDU',
                "url"=>'http://'.$_SERVER['SERVER_NAME'],

                "data"=>[
                    "first"=> [
                        "value"=>"慈善心相悦，公益心相印"."\r\n",
                        "color"=>"#173177"
                    ],
                    "keyword1"=>[
                        "value"=>'爱在同行',
                        "color"=>"#173177"
                    ],
                    "keyword2"=> [
                        "value"=>date('Y-m-d'),
                        "color"=>"#173177"
                    ],
                    "keyword3"=> [
                        "value"=>'0.1元',
                        "color"=>"#173177"
                    ],
                    "remark"=>[
                        "value"=>'爱心接力，牵起爱心的手',
                        "color"=>"#173177"
                    ]
                ]
            ];

            app('EasyWechatOfficial')->template_message->send($data);
        }
    }
}
